package com.cyandpos.pad.pay.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.cyandpos.pad.R;
import com.cyandpos.pad.posdata.Payment;

import java.util.ArrayList;
import java.util.List;

/**
 * 收款方式adapter
 */

public class PayGridAdapter extends BaseAdapter {
    private Context context;
    private List<Payment> skfs = new ArrayList<Payment>();
    private int clickTemp = -1;//标识被选择的item
    private int[] clickedList=new int[30];//这个数组用来存放item的点击状态
    private int[] clicked;
    private boolean mFlag;
    private boolean isMore=true;
    public PayGridAdapter(Context mContext,  List<Payment> mSkfs,boolean isMore) {
        super();
        this.isMore=isMore;
        this.context = mContext;
        this.skfs = mSkfs;
        for (int i=0;i<30;i++){
            clickedList[i]=0;      //初始化item点击状态的数组
        }

    }

    @Override
    public int getCount() {
        if(isMore) {
            return skfs.size() + 1;
        }
        return skfs.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate
                    (R.layout.pay_grid_item, parent, false);
        }
       // ImageView iv = BaseViewHolder.get(convertView, R.id.iv_item);
        TextView tv = BaseViewHolder.get(convertView, R.id.tv_item);
       // iv.setBackgroundResource(imgs.get(position));
        if(position==skfs.size()){
            tv.setText("更多");
            tv.setTextColor(context.getResources().getColor(R.color.main_title_text));
        }else {
            tv.setText(skfs.get(position).getPayName());
            tv.setTextColor(context.getResources().getColor(R.color.main_title_text));
        }
        if(clickTemp==position){    //根据点击的Item当前状态设置背景
            if(mFlag){
              //  iv.setBackgroundResource(select_imgs.get(position));
                tv.setTextColor(context.getResources().getColor(R.color.white) );
                tv.setBackgroundResource(R.drawable.pay_item_bg_true);
            }else if(!mFlag){
              //  iv.setBackgroundResource(imgs.get(position));
                tv.setTextColor(context.getResources().getColor(R.color.main_title_text) );
                tv.setBackgroundResource(R.drawable.pay_item_bg_false);
            }
        }else{
            tv.setTextColor(context.getResources().getColor(R.color.main_title_text) );
            tv.setBackgroundResource(R.drawable.pay_item_bg_false);
        }
        return convertView;
    }
    public void setSelectPos(int position,boolean flag) {
        clickTemp = position;
        mFlag = flag;
        notifyDataSetChanged();
    }
}

